package cn.xopencode.recordservice.service.robotroom;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.convert.robotroom.RobotRoomRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.drytransformer.KvDryTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.drytransformer.KvDryTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.kvtransformer.KvTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.kvtransformer.KvTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.lowroom.LowRoomRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.lowroom.LowRoomRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.maintransformer.MainTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.maintransformer.MainTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.meterroom.MeterRoomRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.meterroom.MeterRoomRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.robotroom.RobotRoomRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.robotroom.RobotRoomRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordOtherDO;
import cn.xopencode.recordservice.dal.mysql.mapper.drytransformer.DryTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.drytransformer.DryTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.kvtransformer.KvTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.kvtransformer.KvTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.lowroom.LowRoomRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.lowroom.LowRoomRecordRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.maintransformer.MainTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.maintransformer.MainTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.meterroom.MeterRoomRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.meterroom.MeterRoomRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.robotroom.RobotRoomRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.robotroom.RobotRoomRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.sf6machine.Sf6MachineRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.sf6machine.Sf6MachineRecordOtherMapper;
import cn.xopencode.recordservice.rpc.robotroom.vo.AdminRecordListVO;
import cn.xopencode.recordservice.rpc.robotroom.vo.RobotRoomRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.robotroom.vo.RobotRoomRecordPageVO;
import cn.xopencode.recordservice.rpc.robotroom.vo.RobotRoomRecordUpdateVO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import cn.xopencode.recordservice.service.robotroom.bo.RobotRoomRecordCreateBO;
import cn.xopencode.recordservice.service.robotroom.bo.RobotRoomRecordPageBO;
import cn.xopencode.recordservice.service.robotroom.bo.RobotRoomRecordUpdateBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXCEPTION;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXITS;

@Service
@Validated
public class RobotRoomRecordService {

    @Autowired
    private RobotRoomRecordMapper robotRoomRecordMapper;
    @Autowired
    private RobotRoomRecordEmpRelMapper robotRoomRecordEmpRelMapper;
    @Autowired
    private Sf6MachineRecordOtherMapper sf6MachineRecordOtherMapper;
    @Autowired
    private Sf6MachineRecordEmpRelMapper sf6MachineRecordEmpRelMapper;
    @Autowired
    private LowRoomRecordRelMapper lowRoomRecordRelMapper;
    @Autowired
    private LowRoomRecordMapper lowRoomRecordMapper;
    @Autowired
    private MainTransformerRecordMapper mainTransformerRecordMapper;
    @Autowired
    private MainTransformerRecordEmpRelMapper mainTransformerRecordEmpRelMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;
    @Autowired
    private KvTransformerRecordMapper kvTransformerRecordMapper;
    @Autowired
    private KvTransformerRecordEmpRelMapper kvTransformerRecordEmpRelMapper;
    @Autowired
    private DryTransformerRecordMapper dryTransformerRecordMapper;
    @Autowired
    private DryTransformerRecordEmpRelMapper dryTransformerRecordEmpRelMapper;
    @Autowired
    private MeterRoomRecordMapper meterRoomRecordMapper;
    @Autowired
    private MeterRoomRecordEmpRelMapper meterRoomRecordEmpRelMapper;

    public PageResult<RobotRoomRecordPageVO> pageRobotRoomRecord(RobotRoomRecordPageBO pageBO){
        IPage<RobotRoomRecordDO> recordDOIPage = robotRoomRecordMapper.selectPage(pageBO);
        PageResult<RobotRoomRecordPageVO> recordPageVOPageResult = RobotRoomRecordConvert.INSTANCE.convert(recordDOIPage);
        if(CollectionUtil.isNotEmpty(recordPageVOPageResult.getList())){
            List<RobotRoomRecordPageVO> list = recordPageVOPageResult.getList();
            for(RobotRoomRecordPageVO vo : list){
                List<RobotRoomRecordEmpRelDO> empRelDOList = robotRoomRecordEmpRelMapper.selectListByRecordId(vo.getId());
                List<String> tmp = empRelDOList.stream().map(RobotRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
                vo.setEmpIds(String.join(",", tmp));
            }
        }
        return recordPageVOPageResult;
    }

    public RobotRoomRecordUpdateVO getRobotRoomRecordById(String id){
        RobotRoomRecordDO recordDO = robotRoomRecordMapper.selectById(id);
        if(recordDO != null){
            RobotRoomRecordUpdateVO updateVO = RobotRoomRecordConvert.INSTANCE.convert(recordDO);
            //查询所有关联的巡检人
            List<RobotRoomRecordEmpRelDO> empRelDOList = robotRoomRecordEmpRelMapper.selectListByRecordId(id);
            updateVO.setEmpList(RobotRoomRecordConvert.INSTANCE.convert(empRelDOList));

            PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(recordDO.getRoomId());
            if(roomDO != null)
                updateVO.setRoomName(roomDO.getRoomName());
            return updateVO;
        }
        return null;
    }

    public Integer createRobotRoomRecord(RobotRoomRecordCreateBO createBO, Set<String> empIds){
        RobotRoomRecordDO recordDO = RobotRoomRecordConvert.INSTANCE.convert(createBO);
        String recordId = IdUtil.objectId();
        recordDO.setId(recordId);
        Integer result = robotRoomRecordMapper.insert(recordDO);
        //保存关联的巡检人信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<RobotRoomRecordEmpRelDO> empRelDOList = empIds.stream()
                    .map(empId -> new RobotRoomRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId).setRecordId(recordId)).collect(Collectors.toList());
            robotRoomRecordEmpRelMapper.insertList(empRelDOList);
        }
        return result;
    }

    public Integer updateRobotRoomRecord(RobotRoomRecordUpdateBO updateBO, Set<String> empIds) {
        RobotRoomRecordDO recordDO = RobotRoomRecordConvert.INSTANCE.convert(updateBO);
        Integer result = robotRoomRecordMapper.updateById(recordDO);
        if(result == null || result != 1){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }

        //先删除之前的关联数据
        robotRoomRecordEmpRelMapper.deleteByRecordId(recordDO.getId());
        //保存关联数据
        if (!CollectionUtil.isEmpty(empIds)) {
            List<RobotRoomRecordEmpRelDO> empRelDOList = empIds.stream()
                    .map(empId -> new RobotRoomRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId).setRecordId(recordDO.getId())).collect(Collectors.toList());
            robotRoomRecordEmpRelMapper.insertList(empRelDOList);
        }
        return result;
    }

    public Integer dealRobotRoomRecordException(String recordId){
        RobotRoomRecordDO recordDO = robotRoomRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return robotRoomRecordMapper.updateById(recordDO);
    }

    /**
     * 后台--根据查询条件 分页获取所有的智能机器人班机柜间巡检记录
     * @param pageBO
     * @return
     */
    public PageResult<RobotRoomRecordAdminPageVO> getAdminPageRobotRoomRecord(AdminRecordPageBO pageBO){
        Page<RobotRoomRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if(pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat(" 00:00:00"));
            pageBO.setInspectionTime2(date.concat(" 23:59:59"));
        }
        IPage<RobotRoomRecordDO> recordDOList = robotRoomRecordMapper.adminQuery(page,pageBO);
        return AdminRecordConvert.INSTANCE.convert(recordDOList);
    }


    /**
     * 获取 后台管理中巡检记录列表
     */
    public List<AdminRecordListVO> getAdminRecordList(){
        List<AdminRecordListVO> list = new ArrayList<>();
        //10KV变压器巡检记录
        KvTransformerRecordDO kvTransformerRecordDO = kvTransformerRecordMapper.getLastRecord();
        AdminRecordListVO listVO = null;
        if(kvTransformerRecordDO != null){
            listVO = AdminRecordConvert.INSTANCE.convert(kvTransformerRecordDO);
            List<KvTransformerRecordEmpRelDO> kvTransformerRecordEmpRelDOList = kvTransformerRecordEmpRelMapper.selectListByRecordId(kvTransformerRecordDO.getId());
            List<String> tmp = kvTransformerRecordEmpRelDOList.stream().map(KvTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO.setEmpIds(String.join(",", tmp));
        }
        listVO.setRecordType("10KV变压器");
        list.add(listVO);

        //10KV干式变压器巡检记录
        KvDryTransformerRecordDO kvDryTransformerRecordDO = dryTransformerRecordMapper.getLastRecord();
        AdminRecordListVO listVO11 = null;
        if(kvDryTransformerRecordDO != null) {
            listVO11 = AdminRecordConvert.INSTANCE.convert(kvDryTransformerRecordDO);
            List<KvDryTransformerRecordEmpRelDO> kvDryTransformerRecordEmpRelDOList = dryTransformerRecordEmpRelMapper.selectListByRecordId(kvDryTransformerRecordDO.getId());
            List<String> tmp0 = kvDryTransformerRecordEmpRelDOList.stream().map(KvDryTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO11.setEmpIds(String.join(",", tmp0));
        }
        listVO11.setRecordType("10KV干式变压器");
        list.add(listVO11);

        //智能机器人班机柜间巡检记录
        RobotRoomRecordDO robotRoomRecordDO = robotRoomRecordMapper.getLastRecord();
        AdminRecordListVO listVO1 = null;
        if(robotRoomRecordDO != null){
            listVO1 = AdminRecordConvert.INSTANCE.convert(robotRoomRecordDO);
            List<RobotRoomRecordEmpRelDO> empRelDOList = robotRoomRecordEmpRelMapper.selectListByRecordId(robotRoomRecordDO.getId());
            List<String> tmp1 = empRelDOList.stream().map(RobotRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO1.setEmpIds(String.join(",", tmp1));
        }
        listVO1.setRecordType("智能机器人班机柜");
        list.add(listVO1);

        //SF6气体密度继电器巡检记录
        Sf6MachineRecordOtherDO otherDO = sf6MachineRecordOtherMapper.getLastRecord();
        AdminRecordListVO listVO2 = null;
        if(otherDO != null){
            listVO2 = AdminRecordConvert.INSTANCE.convert(otherDO);
            List<Sf6MachineRecordEmpRelDO> sf6MachineRecordEmpRelDOList = sf6MachineRecordEmpRelMapper.selectListByRecordCode(otherDO.getRecordCode());
            List<String> tmp2 = sf6MachineRecordEmpRelDOList.stream().map(Sf6MachineRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO2.setEmpIds(String.join(",", tmp2));
        }
        listVO2.setRecordType("SF6气体密度继电器");
        list.add(listVO2);

        //主变巡检记录
        MainTransformerRecordDO mainTransformerRecordDO = mainTransformerRecordMapper.getLastRecord();
        AdminRecordListVO listVO3 = null;
        if(mainTransformerRecordDO != null){
            listVO3 = AdminRecordConvert.INSTANCE.convert(mainTransformerRecordDO);
            List<MainTransformerRecordEmpRelDO> mainTransformerRecordEmpRelDOList = mainTransformerRecordEmpRelMapper.selectListByRecordId(mainTransformerRecordDO.getId());
            List<String> tmp3 = mainTransformerRecordEmpRelDOList.stream().map(MainTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO3.setEmpIds(String.join(",", tmp3));
        }
        listVO3.setRecordType("主变");
        list.add(listVO3);

        //低压配电室巡检记录
        LowRoomRecordDO lowRoomRecordDO = lowRoomRecordMapper.getLastRecord();
        AdminRecordListVO listVO4 = null;
        if(lowRoomRecordDO != null){
            listVO4 = AdminRecordConvert.INSTANCE.convert(lowRoomRecordDO);
            List<LowRoomRecordEmpRelDO> lowRoomRecordEmpRelDOList = lowRoomRecordRelMapper.selectListByLowRoomRecordId(lowRoomRecordDO.getId());
            List<String> tmp4 = lowRoomRecordEmpRelDOList.stream().map(LowRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO4.setEmpIds(String.join(",", tmp4));
        }
        listVO4.setRecordType("低压配电室");
        list.add(listVO4);

        //仪表机柜间巡检记录
        MeterRoomRecordDO meterRoomRecordDO = meterRoomRecordMapper.getLastRecord();
        AdminRecordListVO listVO5 = null;
        if(meterRoomRecordDO != null){
            listVO5 = AdminRecordConvert.INSTANCE.convert(meterRoomRecordDO);
            List<MeterRoomRecordEmpRelDO> meterRoomRecordEmpRelDOList = meterRoomRecordEmpRelMapper.selectListByRecordId(meterRoomRecordDO.getId());
            List<String> tmp4 = meterRoomRecordEmpRelDOList.stream().map(MeterRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
            listVO5.setEmpIds(String.join(",", tmp4));
        }
        listVO5.setRecordType("仪表机柜间");
        list.add(listVO5);

        return list;
    }

}
